/*
 * Forum.java
 *
 * Created on September 10, 2006, 3:59 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package smfbackup.conf;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.Vector;
import smfbackup.Main;
import smfbackup.Utils;

/**
 *
 * @author Iulian
 */
public class Forum implements Serializable {

//    private Logger log = Logger.getLogger(Forum.class);

    private String name;
    private String url;
    private String username;
    private String pass;
    private String interval; // daily
    private String startAt;  // hour
    private String repeatEvery; // 6 hours
    private boolean doBackupWhenForumStarts;
    private boolean disableBackups;
    private boolean doCompression;
    private int version;
    private Vector<String> status;
    private long downloadCount;
    private boolean loadedFromDisk = false;
    private SimpleDateFormat df;

    /** Creates a new instance of Forum */
    public Forum() {
        status = new Vector<String>();
        downloadCount = 0;
        df = new SimpleDateFormat("[dd/MM HH:mm:ss]");
    }

    public void setDefaults() {
        setName("New Forum");
        setUrl("http://.../index.php");
        setUsername("admin");
        setPass("somepass");
        setInterval("daily");
        setStartAt("13:45");
        setRepeatEvery("2");
        setDoBackupWhenForumStarts(true);
        setDisableBackups(false);
        setDoCompression(true);
        setVersion(0);
    }

    public void updateDownloadCount(long newDownloadCount) {
        downloadCount = newDownloadCount;
        Main.getMainWindow().fireLogUpdate();
    }

    public void updateStatus(String changed) {
        StringTokenizer st = new StringTokenizer(changed, "\n");
        status.addElement(df.format(new Date()) + " " + st.nextToken());
        while (st.hasMoreTokens()) {
            status.addElement(st.nextToken());
        }

        Main.getMainWindow().fireLogUpdate();
    }

    public long getDownloadCount() {
        return downloadCount;
    }

    public String getStatus(int linesAlreadyWritten) {
        StringBuffer sb = new StringBuffer();

        for (int i = linesAlreadyWritten; i < status.size(); i++) {
            sb.append((String) status.elementAt(i) + System.getProperty("line.separator"));
        }

        return sb.toString();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public String getStartAt() {
        return startAt;
    }

    public void setStartAt(String startAt) {
        this.startAt = startAt;
    }

    public String getRepeatEvery() {
        return repeatEvery;
    }

    public void setRepeatEvery(String repeatEvery) {
        this.repeatEvery = repeatEvery;
    }

    public Properties save(String prefix, int index) {
        Properties conf = new Properties();

        conf.setProperty(prefix + "." + index + "." + "name", name);
        conf.setProperty(prefix + "." + index + "." + "url", url);
        conf.setProperty(prefix + "." + index + "." + "username", username);
        conf.setProperty(prefix + "." + index + "." + "password", Utils.encrypt(pass));
        conf.setProperty(prefix + "." + index + "." + "inteval", interval);
        conf.setProperty(prefix + "." + index + "." + "startat", startAt);
        conf.setProperty(prefix + "." + index + "." + "repeatevery", repeatEvery);
        conf.setProperty(prefix + "." + index + "." + "runatstart", "" + doBackupWhenForumStarts);
        conf.setProperty(prefix + "." + index + "." + "disable", "" + disableBackups);
        conf.setProperty(prefix + "." + index + "." + "version", "" + version);
        conf.setProperty(prefix + "." + index + "." + "compression", "" + doCompression);

        return conf;
    }

    public void load(Properties conf, String prefix, int index) {
        setName(conf.getProperty(prefix + "." + index + "." + "name"));
        setUrl(conf.getProperty(prefix + "." + index + "." + "url"));
        setUsername(conf.getProperty(prefix + "." + index + "." + "username"));
        setPass(Utils.decrypt(conf.getProperty(prefix + "." + index + "." + "password")));
        setInterval(conf.getProperty(prefix + "." + index + "." + "inteval"));
        setStartAt(conf.getProperty(prefix + "." + index + "." + "startat"));
        setRepeatEvery(conf.getProperty(prefix + "." + index + "." + "repeatevery"));
        setDoBackupWhenForumStarts(Boolean.parseBoolean(conf.getProperty(prefix + "." + index + "." + "runatstart")));
        setDisableBackups(Boolean.parseBoolean(conf.getProperty(prefix + "." + index + "." + "disable")));
        setVersion(Integer.parseInt(conf.getProperty(prefix + "." + index + "." + "version")));

        // assume that compress is default if not specified in the configuration
        String sDoCompress = conf.getProperty(prefix + "." + index + "." + "compression");
        if (sDoCompress == null || sDoCompress.trim().length() == 0) {
            setDoCompression(true);
        } else {
            setDoCompression(Boolean.parseBoolean(sDoCompress));
        }

        loadedFromDisk = true;
    }

    public boolean isDoBackupWhenForumStarts() {
        return loadedFromDisk && doBackupWhenForumStarts;
    }

    public void setDoBackupWhenForumStarts(boolean doBackupWhenForumStarts) {
        this.doBackupWhenForumStarts = doBackupWhenForumStarts;
    }

    public boolean isDisableBackups() {
        return disableBackups;
    }

    public void setDisableBackups(boolean disableBackups) {
        this.disableBackups = disableBackups;
    }

    @Override
    public String toString() {
        return name;
    }

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    public void setDoCompression(boolean doCompression) {
        this.doCompression = doCompression;
    }

    public boolean isDoCompression() {
        return doCompression;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Forum other = (Forum) obj;

        if (isDistinct(this.name, other.name) ||
                isDistinct(this.url, other.url) ||
                isDistinct(this.username, other.username) ||
                isDistinct(this.pass, other.pass) ||
                isDistinct(this.interval, other.interval) ||
                isDistinct(this.startAt, other.startAt) ||
                isDistinct(this.repeatEvery, other.repeatEvery) ||
                (this.doBackupWhenForumStarts != other.doBackupWhenForumStarts) ||
                (this.disableBackups != other.disableBackups) ||
                (this.doCompression != other.doCompression) ||
                (this.version != other.version) ||
                (this.loadedFromDisk != other.loadedFromDisk)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 97 * hash + (this.url != null ? this.url.hashCode() : 0);
        hash = 97 * hash + (this.username != null ? this.username.hashCode() : 0);
        hash = 97 * hash + (this.pass != null ? this.pass.hashCode() : 0);
        hash = 97 * hash + (this.interval != null ? this.interval.hashCode() : 0);
        hash = 97 * hash + (this.startAt != null ? this.startAt.hashCode() : 0);
        hash = 97 * hash + (this.repeatEvery != null ? this.repeatEvery.hashCode() : 0);
        hash = 97 * hash + (this.doBackupWhenForumStarts ? 1 : 0);
        hash = 97 * hash + (this.disableBackups ? 1 : 0);
        hash = 97 * hash + (this.doCompression ? 1 : 0);
        hash = 97 * hash + this.version;
        hash = 97 * hash + (this.loadedFromDisk ? 1 : 0);
        return hash;
    }

    private boolean isDistinct(Comparable val1, Comparable val2) {
        if (val1 == val2) {
            return false;
        }
        if (val1 != null) {
            return val1.compareTo(val2) != 0;
        } else {
            return val2.compareTo(val1) != 0;
        }
    }
}
